#ifndef ARDUINOCONTROLLERDEVICE_H
#define ARDUINOCONTROLLERDEVICE_H

#include "IODeviceWithThreadSupport.h"
#include "3rd/serial/include/serial/serial.h"
#include <BDDeque.h>

#define BUTTON_FRAME_LENGTH           5
#define ROCKER_FRAME_LENGTH           8

#define START_CODE             0XBD
#define END_CODE               0XDB

#define START_CODE_INDEX_1     0
#define START_CODE_INDEX_2     1

#define KIND_CODE_INDEX        2
#define KIND_CODE_BUTTON_BIT   0
#define KIND_CODE_ROCKER_BIT   1

#define BUTTON_INDEX           3
#define BUTTON_YELLOW_BIT      0
#define BUTTON_RED_BIT         1
#define BUTTON_GREEN_BIT       2

#define END_CODE_BUTTON_INDEX  4

#define ROCKER_X1_INDEX         3
#define ROCKER_Y1_INDEX         4
#define ROCKER_X2_INDEX         5
#define ROCKER_Y2_INDEX         6

#define END_CODE_ROCKER_INDEX  7

#define BUTTON_YELLOW_PIN      5
#define BUTTON_RED_PIN         6
#define BUTTON_GREEN_PIN       7

#define ROCKER_X1_PIN          0
#define ROCKER_Y1_PIN          1
#define ROCKER_X2_PIN          2
#define ROCKER_Y2_PIN          3

class ArduinoControllerDevice :
	public IODeviceWithThreadSupport
{
public:

	ArduinoControllerDevice(IODeviceContext* context);
	virtual ~ArduinoControllerDevice();

	int Init();

	int DeInit();

	void OnOperation(IODeviceOperation operation, unsigned int param);

private:

	BDDeque<uint8_t> m_Data;

	serial::Serial* m_pSerial;

	void ReadData(BDDeque<uint8_t>& data);

	int HandleButtonData(BDDeque<uint8_t>& data, ArduinoControllerData& output);

	int HandleRockerData(BDDeque<uint8_t>& data, ArduinoControllerData& output);
};

#endif
